package com.chinacoal.hr.levelimpl.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinacoal.hr.levelapi.entity.HolidayLaws;
import com.chinacoal.hr.levelimpl.mapper.HolidayLawsMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-19 01:53
 * @description:
 **/
@Component
public class HolidayLawsDao {

    @Resource
    private HolidayLawsMapper holidayLawsMapper;

    /**
     * 查询假期排班俩表
     *
     * @param condition :
     * @return : com.baomidou.mybatisplus.core.metadata.IPage<com.chinacoal.hr.levelimpl.entity.BizTravel>
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    public IPage<HolidayLaws> getHolidayLawList(Map<String, Object> condition) throws Throwable {
        Long currentPage = (Long) condition.get("page");
        Long currentPageSize = (Long) condition.get("limit");

        IPage<HolidayLaws> page = new Page(currentPage, currentPageSize);

        String orgId = condition.get("orgId") != null ? condition.get("orgId").toString() : "";
        String year = condition.get("year") != null ? condition.get("year").toString() : "";
        String hDate = condition.get("hDate") != null ? condition.get("hDate").toString() : "";

        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(orgId)) {
            queryWrapper.eq("OU_ID", orgId);
        }

        if (StringUtils.isNotBlank(year)) {
            queryWrapper.eq("YEAR", year);
        }

        if (StringUtils.isNotBlank(hDate)) {
            queryWrapper.eq("H_DATE", hDate);
        }

        queryWrapper.orderByDesc("H_DATE");


        IPage<HolidayLaws> bizTravelList = holidayLawsMapper.selectPage(page, queryWrapper);

        return bizTravelList;
    }

    /**
     * 新增假期排版信息
     *
     * @param record :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    public void insertHolidayLaw(HolidayLaws record) {
        holidayLawsMapper.insert(record);
    }

    /**
     * 更新假期排版信息
     *
     * @param record :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    public void updateHolidayLaw(HolidayLaws record) {
        this.holidayLawsMapper.updateById(record);
    }

    /**
     * 删除假期排版信息
     *
     * @param bizTraveId :
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/12/18 2019/12/18
     */
    public void deleteHolidayLawById(String bizTraveId) {
        holidayLawsMapper.deleteById(bizTraveId);
    }

    /**
     * 按条件查询，如果没有排班信息则返回空
     *
     * @param condition :
     *                  hDate:yyyy-MM-dd
     * @return : void
     * @author : YunTao.Li
     * @date : 2020/7/23 2020/7/23
     */
    public HolidayLaws getHolydayLow(Map condition) {
        String hDate = condition.get("hDate") != null ? condition.get("hDate").toString() : "";
        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(hDate)) {
            String sql = "UNIX_TIMESTAMP('" + hDate + " 00:00:00') <= UNIX_TIMESTAMP(H_DATE)" +
                    "and UNIX_TIMESTAMP('" + hDate + " 23:59:59') >= UNIX_TIMESTAMP(H_DATE)";
            queryWrapper.apply(sql);
        }

        queryWrapper.last("limit 1");
        HolidayLaws holidayLaws = holidayLawsMapper.selectOne(queryWrapper);
        return holidayLaws;
    }

}
